package com.chen.demo.javase.juc;

import sun.lwawt.macosx.CSystemTray;

import java.sql.Time;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class WaitDemo {

    private static final Object monitor = new Object();

    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws InterruptedException {

       /* ConsumerThread consumerThread = new ConsumerThread();
        consumerThread.setName("consumerThread");
        consumerThread.start();


        ConsumerThread consumerThread2 = new ConsumerThread();
        consumerThread2.setName("consumerThread2");
        consumerThread2.start();


        TimeUnit.SECONDS.sleep(2);
        //等待
        ProducerThread producerThread = new ProducerThread();

        producerThread.start();*/

        waitTimeOutThread waitTimeOutThread = new waitTimeOutThread();
        waitTimeOutThread.start();
        TimeUnit.SECONDS.sleep(1);
        synchronized (monitor){
            System.out.println("主线程获得锁，sleep 50s");
            TimeUnit.SECONDS.sleep(5);
        }
        countDownLatch.await();
    }


    public static class ProducerThread extends Thread {
        @Override
        public void run() {
            while (true){
                synchronized (monitor){
                    System.out.println(this.getName()+",消息生产完毕，通知消费者,time:"+System.currentTimeMillis()+"\t\t");
                    monitor.notifyAll();
                    System.out.println("生产者调用notifyAll,暂停5s释放锁.");
                    try {
                        TimeUnit.SECONDS.sleep(5);
                        // 如果此处sleep,消费者是否可以获取到锁
                        // TimeUnit.HOURS.sleep(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(this.getName()+",生产者锁释放");
            }

        }
    }

    public static class ConsumerThread extends Thread {
        @Override
        public void run() {
            while (true){
                synchronized (monitor){
                    System.out.println(this.getName()+",消息消费完成，等待生产者通知\t\t");
                    try {

                        monitor.wait();

                        for (int i = 0; i < 10000; i++){
                            System.out.print(this.getName()+",i:"+i+";");
                        }
                        System.out.print(this.getName()+",wait被唤醒.");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }


    public static class waitTimeOutThread extends Thread{

        @Override
        public void run() {
            synchronized (monitor){
                try {
                    System.out.println("waitTimeOutThread 获取到锁");
                    monitor.wait(3000);
                    System.out.println("wait被唤醒");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
